var feed;

var datasetList;

var randomInterval;

var aveWaitingTime = new Array();
aveWaitingTime[0] = 6
aveWaitingTime[1] = 7
aveWaitingTime[2] = 4
aveWaitingTime[3] = 7
aveWaitingTime[4] = 8

var names = new Array();
names[0] = "Big Burger"
names[1] = "Pizza Place"
names[2] = "Food Court"
names[3] = "Chinese Cuisine"
names[4] = "Indian Food"


function RestaurantInfoDemo(){   
    feed = SAPFeed("http://10.33.247.153:8000", this, 23, "aaa");

    
    this.onSAPFeedLogin = function(){
        datasetList = [];
        
        datasetList.push(13);

        $('#status_bar').append('<p>'+'Logged In'+'</p>');
        
        randomPublish();
    }
    this.onSAPFeedLogout = function(){
        $('#status_bar').append('<p>'+'Logged Out'+'</p>');
    }
    
    this.onSAPFeedError = function(responseReceived){
        $('#status_bar').append('<p>'+responseReceived.code+": "+responseReceived.message+'</p>');

    }

    this.onSAPFeedPublish = function(){
        randomInterval = Math.floor(4000*Math.random());
        setTimeout(function(){
            randomPublish();
        }, randomInterval)
    }
    
    function randomPublish(){

        var rID = Math.floor(5*Math.random());   
        var tmp = Math.random();
        if(tmp>0.7)
            aveWaitingTime[rID] = aveWaitingTime[rID]+(Math.ceil(3*Math.random()));
        else
            aveWaitingTime[rID] = aveWaitingTime[rID]-(Math.ceil(3*Math.random()));
   
        var message = "Normal";
        
        if(aveWaitingTime[rID]<=1){
            message = "Available";
            aveWaitingTime[rID] = 0;
        }

        if(aveWaitingTime[rID]>=10){
            message = "Crowded";

        }
        
        feed.publish(datasetList, {id: rID, name: names[rID], time: aveWaitingTime[rID], message: message});

    }

}
$(document).ready(function(){
    RestaurantInfoDemo();
    $('#stop_btn').click(function(){
        feed.logout();
    });
});